home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / utilities / viewconv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  16.0 KB  |  511 lines

  1.  
  2. // File to create a window interface for ViewConvert
  3.  
  4.  
  5. #include <windows.h>
  6. #include "std.h" 
  7. #include "dvstd.h"
  8. #include "dvtools.h"
  9. #include "Tfundecl.h"
  10. #include "VOstd.h"
  11.  
  12. CHAR *ProgName = __FILE__;
  13. VIEW view;
  14. BOOL save_as_ascii,
  15.          convert_dynamics;
  16. CHAR *path;
  17. BOOL result;
  18.  
  19.  
  20. long APIENTRY  WndProc(HWND, UINT, UINT, LONG);
  21. long APIENTRY  HlpWndProc(HWND, UINT, UINT, LONG);
  22. int vwcnvrt( int, int, int, char *, CHAR * );
  23. void translate_args( int argc, char **argv, int *atob, int *btoa,
  24.                 int *dyn_convert,  char **output_view, char **input_view);
  25.  
  26. #define EDITID 1
  27. #define LOWORD   /* Program won't work without this macro redefined */
  28.  
  29. HWND      hwnd;
  30. HWND hwndHlp, hwndClose;
  31. HANDLE hInstan, hInst;
  32.  
  33. char *messages[6] = {
  34.                       " View converted. ",
  35.                       " Error saving view. View not saved.",
  36.                       " Error loading view. Conversion not done.",
  37.                       " warning: could not terminate cleanly.",
  38.                       "error in initialization. conversion not done"
  39.                     };
  40.                      
  41.  
  42.  
  43.  
  44.  
  45. int APIENTRY WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
  46.              int nCmdShow )
  47. {
  48.  
  49.  
  50.   MSG       msg;
  51.   WNDCLASS  wndclass;
  52.   char * szAppName = "vc";
  53.   char * class_name = "ViewConvert";
  54.   int        argc=0;
  55.   char     **argv;  
  56.                           
  57.  
  58.   hInstan = hInstance;
  59.  
  60.   make_argv(&argc,&argv,GetCommandLine());
  61.  
  62.   if ( argc > 2 ) 
  63.  {
  64.     int   atob=0;           /* true for ascii to binary conv. */
  65.     int   btoa=0;           /* true for binary to ascii conv. */
  66.     int   dyn_convert=0;    /* true to convert dynamics       */
  67.     char *input_name;       /* source view name               */
  68.    char *output_name;         /* output view name               */
  69.  
  70.  
  71.     /* if enough command line args are given, convert them */
  72.     /* and call vwcnvrt, otherwise use davids' dialog to   */
  73.     /* get the args needed */
  74.    translate_args(argc,argv,&atob,&btoa,&dyn_convert,&output_name,&input_name);
  75.    vwcnvrt( atob, btoa, dyn_convert, input_name,output_name );
  76.   }
  77.   else 
  78.   { 
  79.  if (!hPrevInstance)
  80.        {
  81.         wndclass.style         =  CS_HREDRAW | CS_VREDRAW;
  82.         wndclass.hInstance     =  hInstance;
  83.         wndclass.lpszClassName  =  class_name;
  84.         wndclass.lpszMenuName  =  "dvmenu";
  85.         wndclass.hIcon         =  LoadIcon (NULL, IDI_APPLICATION);
  86.         wndclass.hCursor       =  LoadCursor (NULL, IDC_ARROW);
  87.         wndclass.hbrBackground =  GetStockObject(WHITE_BRUSH);
  88.         wndclass.lpfnWndProc   =  WndProc;
  89.         wndclass.cbClsExtra    =  0;
  90.         wndclass.cbWndExtra    =  0;
  91.  
  92.         RegisterClass(&wndclass);
  93.        }
  94.  
  95.    hwnd = CreateWindow( class_name, class_name,
  96.                         WS_OVERLAPPEDWINDOW,
  97.                         CW_USEDEFAULT,
  98.                         CW_USEDEFAULT,
  99.                         640, 340,
  100.                         NULL, 
  101.                         NULL, 
  102.                         hInstance,
  103.                         NULL );
  104.   
  105.    ShowWindow( hwnd, nCmdShow );
  106.   
  107.    UpdateWindow( hwnd );
  108.  
  109.    while (GetMessage( &msg, NULL, 0, 0 ))
  110.         {
  111.           TranslateMessage( &msg );
  112.           DispatchMessage( &msg );
  113.         }
  114.    return msg.wParam;
  115.   }
  116. }
  117.  
  118. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, UINT wParam, LONG lParam)
  119. {
  120.   HDC hdc;
  121.   PAINTSTRUCT ps;
  122.   RECT rect;
  123.   HPEN hPen, oldPen;
  124.  static HWND hwndButtonA, hwndButtonB, hwndButtonC; 
  125.  static HWND hwndEditA, hwndEditB, hwndOK, hwndClear, hwndCancel, hwndHelp;
  126.   static int cxChar, cyChar;
  127.   int t_length = 0;
  128.   TEXTMETRIC tm;
  129.   char * string = "off";
  130.   WORD A_check = 0;
  131.   WORD B_check = 0;
  132.   char * text_in;
  133.   char * text_out = "";  
  134.   static int atob = 0;
  135.   static int btoa = 0;
  136.   static int dyn_convert = 0;
  137.         
  138.  
  139.   switch (message)
  140.   {
  141.      case  WM_CREATE :
  142.           hdc = GetDC(hwnd);
  143.           SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
  144.           GetTextMetrics(hdc, &tm);
  145.           cxChar = tm.tmAveCharWidth;
  146.           cyChar = tm.tmHeight + tm.tmExternalLeading;
  147.           
  148.  
  149.          hwndButtonA = CreateWindow("button", "  ascii to binary", 
  150.                                      WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
  151.                                      cxChar, cyChar, (20 * cxChar),
  152.                                      (7 * cyChar / 4), hwnd, 0, 
  153.                                      ((LPCREATESTRUCT)lParam)->hInstance, 
  154.                                       NULL);
  155.  
  156.          hwndButtonB = CreateWindow("button", "  binary to ascii", 
  157.                                      WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
  158.                                      cxChar, cyChar + 60, (20 * cxChar),
  159.                                      (7 * cyChar / 4), hwnd, 0, 
  160.                                      ((LPCREATESTRUCT)lParam)->hInstance, 
  161.                                       NULL);
  162.  
  163.          hwndButtonC = CreateWindow("button", " Pre 8.0 dynamics", 
  164.                                      WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
  165.                                      cxChar, cyChar + 120, (20 * cxChar),
  166.                                      (7 * cyChar / 4), hwnd, 0, 
  167.                                      ((LPCREATESTRUCT)lParam)->hInstance, 
  168.                                       NULL);
  169.           
  170.          hPen = GetStockObject(WHITE_PEN);
  171.          
  172.          oldPen = SelectObject(hdc, hPen);
  173.          TextOut(hwnd, cxChar + 200, cyChar + 400, "From", 7);
  174.          SelectObject(hdc, oldPen);
  175.        
  176.  
  177.          hwndEditA = CreateWindow("edit", NULL,
  178.                            ES_AUTOHSCROLL | WS_BORDER | WS_CHILD | WS_VISIBLE ,
  179.                                   cxChar, cyChar + 220, 120, 60, hwnd, 
  180.                                    (HMENU) 1,
  181.                                    ((LPCREATESTRUCT)lParam)->hInstance,
  182.                                    NULL );
  183.  
  184.          hwndEditB = CreateWindow("edit", NULL,
  185.                            ES_AUTOHSCROLL | WS_BORDER | WS_CHILD | WS_VISIBLE ,
  186.                                   cxChar + 200, cyChar + 220, 120, 60, hwnd,
  187.                                   (HMENU) 1,
  188.                                   ((LPCREATESTRUCT)lParam)->hInstance,
  189.                                   NULL );
  190.  
  191.          hwndOK  = CreateWindow( "button", " OK ", 
  192.                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  193.                                   cxChar + 500, cyChar, 80, 60, hwnd, 0,
  194.                                   ((LPCREATESTRUCT)lParam)->hInstance, 
  195.                                   NULL );
  196.          
  197.          hwndClear  = CreateWindow( "button", " CLEAR ", 
  198.                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  199.                                   cxChar + 500, cyChar+60, 80, 60, hwnd, 0,
  200.                                   ((LPCREATESTRUCT)lParam)->hInstance, 
  201.                                   NULL );
  202.  
  203.          hwndCancel  = CreateWindow( "button", " EXIT ", 
  204.                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  205.                                   cxChar + 500, cyChar+120, 80, 60, hwnd, 0,
  206.                                   ((LPCREATESTRUCT)lParam)->hInstance, 
  207.                                   NULL );
  208.       
  209.          hwndHelp  = CreateWindow( "button", " HELP ", 
  210.                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  211.                                   cxChar + 500, cyChar+180, 80, 60, hwnd, 0,
  212.                                   ((LPCREATESTRUCT)lParam)->hInstance, 
  213.                                   NULL );
  214.  
  215.         
  216.         
  217.           SendMessage(hwndButtonA, BM_SETCHECK, 1, 0L);
  218.         ReleaseDC(hwnd, hdc);      
  219.         return 0;
  220.  
  221.  
  222.      case WM_COMMAND :
  223.  
  224.        if ((LOWORD(lParam) == (long)hwndEditA) || (LOWORD(lParam) == (long)hwndEditB))  
  225.         return 0;      
  226.     
  227.        if (LOWORD(lParam) == (long)hwndCancel)
  228.           exit(0);
  229.  
  230.        if (LOWORD(lParam) == (long)hwndClose)
  231.           CloseWindow( hwndHlp );
  232.  
  233.        if (LOWORD(lParam) == (long)hwndClear)
  234.        {
  235.           (WORD)SendMessage(hwndButtonA, BM_SETCHECK, 0, 0L);
  236.           (WORD)SendMessage(hwndButtonB, BM_SETCHECK, 0, 0L);
  237.           (WORD)SendMessage(hwndButtonC, BM_SETCHECK, 0, 0L);
  238.           SetWindowText(hwndEditA, "");
  239.           SetWindowText(hwndEditB, "");
  240.        }
  241.        
  242.        if (LOWORD(lParam) == (long)hwndHelp)
  243.        {
  244.           HWND hwndHlp;
  245.           HDC hdc;
  246.           WNDCLASS hlpclass;
  247.           LPCTSTR help_text;
  248.           RECT rect;
  249.  
  250.  
  251.          hlpclass.style         =  CS_HREDRAW | CS_VREDRAW;
  252.          hlpclass.hInstance     =  hInst;
  253.          hlpclass.lpszClassName  =  "help";
  254.          hlpclass.lpszMenuName  =  "ViewConvert Help";
  255.          hlpclass.hIcon         =  LoadIcon (NULL, IDI_APPLICATION);
  256.          hlpclass.hCursor       =  LoadCursor (NULL, IDC_ARROW);
  257.          hlpclass.hbrBackground =  GetStockObject(WHITE_BRUSH);
  258.          hlpclass.lpfnWndProc   =  DefWindowProc;
  259.          hlpclass.cbClsExtra    =  0;
  260.          hlpclass.cbWndExtra    =  0;
  261.  
  262.          RegisterClass(&hlpclass);
  263.  
  264.          hwndHlp =  CreateWindow( "help", "HELP",
  265.                         WS_OVERLAPPEDWINDOW,
  266.                         CW_USEDEFAULT,
  267.                         CW_USEDEFAULT,
  268.                         640, 340,
  269.                         NULL, 
  270.                         NULL, 
  271.                         hInst,
  272.                         NULL );
  273.  
  274.          ShowWindow(hwndHlp, SW_SHOWNORMAL);
  275.  
  276.          help_text = "VIEWCONV.EXE - View Conversion Utility                 \n \
  277.  \n \
  278. This program converts a view file from binary to ASCII or from ASCII         \n \
  279. to binary save format.                                                       \n \
  280. Select the \"Binary to ASCII\" or \"ASCII to Binary\" check box to specify   \n \
  281. the type of conversion. \n \
  282. Select the \"Pre 8.0 Dynamics\" check box only if your view contains dynamics \n \
  283. created using a version of DataViews prior to 8.0. \n \
  284. \n \
  285. Type the name of the view file to be converted in the left text box and its  \n \
  286. new file name in the right text box. Leaving the right text box empty will \n \
  287. result in the same filename being used. Full path names may be used to indicate  \n \
  288. view files. \n \
  289. \n \
  290. Select \"OK\" to execute the conversion. \n \
  291. Select \"CLEAR\" to clear all text from the screen without exiting the program. \n \
  292. Select \"EXIT\" to exit the program without performing the conversion. \n  ";
  293.  
  294.          rect.left = 10;
  295.          rect.top = 10;
  296.          rect.right = 640;
  297.          rect.bottom = 400;
  298.                                 
  299.          hdc = GetDC(hwndHlp);
  300.          DrawText(hdc, help_text, -1, &rect, 0); 
  301.          ReleaseDC(hwndHlp, hdc);
  302.              
  303.  
  304.        }
  305.  
  306.       if (LOWORD(lParam) != (long)hwndButtonC)
  307.             SendMessage((HWND)LOWORD(lParam), BM_SETCHECK,
  308.                 (WORD)!SendMessage((HWND)LOWORD(lParam), BM_GETCHECK, 0, 0L), 0L);
  309.  
  310.           A_check = (WORD)SendMessage(hwndButtonA, BM_GETCHECK, 0, 0L);
  311.           B_check = (WORD)SendMessage(hwndButtonB, BM_GETCHECK, 0, 0L);
  312.           dyn_convert = (WORD)SendMessage(hwndButtonC, BM_GETCHECK, 1, 0L);
  313.  
  314.           if (LOWORD(lParam) == (long)hwndButtonA)
  315.           {
  316.               SendMessage(hwndButtonA, BM_SETCHECK, 1, 0L);
  317.               SendMessage(hwndButtonB, BM_SETCHECK, 0, 0L);
  318.               atob = TRUE;
  319.               btoa = FALSE;
  320.            }    
  321.           else if (LOWORD(lParam) == (long)hwndButtonB)
  322.           {
  323.             SendMessage(hwndButtonB, BM_SETCHECK, 1, 0L);
  324.             SendMessage(hwndButtonA, BM_SETCHECK, 0, 0L);
  325.             atob = FALSE;
  326.             btoa = TRUE;
  327.           }
  328.  
  329.          
  330.  
  331.           if((WORD)SendMessage( hwndOK, BM_GETSTATE, 1, 0L ))
  332.           {
  333.             int result=0;
  334.             t_length = GetWindowTextLength( hwndEditA );
  335.             if (t_length == 0)
  336.             {
  337.              MessageBox(hwnd, "A filename must be provided.",
  338.                         "ViewConvert", MB_APPLMODAL );
  339.              return 0;          
  340.             }
  341.             text_in = (char *)malloc(t_length+1);
  342.             GetWindowText( hwndEditA, text_in, t_length+1 );
  343.         
  344.             t_length = GetWindowTextLength( hwndEditB );
  345.             text_out = (char *)malloc(t_length+1);
  346.             GetWindowText( hwndEditB, text_out, t_length+1 );
  347.  
  348.             result = vwcnvrt( atob, btoa, dyn_convert, text_in, text_out ); 
  349.             MessageBox(hwnd,messages[result],"ViewConvert",MB_APPLMODAL);
  350.           }
  351.         
  352.           return 0;
  353.  
  354.      case WM_PAINT :
  355.           hdc = BeginPaint( hwnd, &ps );
  356.  
  357.           GetClientRect( hwnd, &rect );
  358.           TextOut(hdc, 11, 220, "From", 4); 
  359.           TextOut(hdc, 211, 220, "To", 2);         
  360.           EndPaint( hwnd, &ps );
  361.  
  362.           return 0;
  363.  
  364.      case WM_SETFOCUS :
  365.           SetFocus(hwnd);
  366.           return 0;
  367.   
  368.      case WM_DESTROY :
  369.           PostQuitMessage (0);
  370.           return 0;
  371.   }
  372.   return DefWindowProc( hwnd, message, wParam, lParam );
  373. }
  374.  
  375.  
  376.  
  377. int vwcnvrt( int atob, int btoa, int dyn_convert, char * text_in, char * text_out )
  378. {
  379.    LOCAL CHAR *Defaultpath = NULL;    /* Use value of V_config var DVPATH */
  380.    LOCAL CHAR * view_name;
  381.    LOCAL CHAR * converted_view = "";
  382.    INT save_as_ascii, len;
  383.    INT convert_dynamics;
  384.    int ret_val;
  385.  
  386.  
  387.     ret_val = 0;
  388.     view_name = text_in;
  389.     save_as_ascii = btoa;
  390.     convert_dynamics = dyn_convert;
  391.  
  392.     len = strlen(text_out);
  393.     if (len)
  394.     {
  395.      converted_view = (char *)malloc(len+1);
  396.      strcpy(converted_view, text_out);
  397.     }
  398.     else
  399.     {
  400.      converted_view = (char *)malloc(strlen(text_in)+1);
  401.      strcpy(converted_view, text_in);
  402.     }
  403.  
  404.     path = Defaultpath;
  405.  
  406.     if (!TInit (path, (CHAR *)NULL))
  407.       {
  408.         
  409.          ret_val = 4;
  410.          return( ret_val );
  411.       }
  412.     
  413.     /* get source view */
  414.     view = TviLoad (view_name); 
  415.  
  416.   
  417.     if (view)
  418.       {
  419.       if (convert_dynamics && TviTestDynamics(view))
  420.         TviConvertDynamics(view);
  421.       if (save_as_ascii) result = TviASCIISave(view, converted_view);
  422.       else result = TviSave(view, converted_view);
  423.       if (!result)
  424.           ret_val = 1;    
  425.       else
  426.             ret_val = 0;
  427.           
  428.       }
  429.     else
  430.       {
  431.         ret_val = 2;
  432.     
  433.       (VOID)TTerminate();
  434.     
  435.       }
  436.     
  437.     /* tidy up and exit */
  438.     if (!TviDestroy( view ))
  439.       {
  440.       (VOID)fprintf (stderr, " warning: could not destroy view.\n",
  441.                  ProgName);
  442.       (VOID)TTerminate();
  443.       
  444.       }
  445.     else if (!TTerminate())
  446.       {
  447.         ret_val = 3; 
  448.    
  449.       }
  450.  
  451.   return( ret_val );
  452. }
  453.  
  454. /**                                                       **/
  455. /** translate command line args. This set the variables   **/
  456. /** needed to call the function vwcnvrt                   **/
  457. /**                                                       **/   
  458. void translate_args( int argc, char **argv, int *atob, int *btoa,
  459.                 int *dyn_convert,  char **output_view, char **input_view)
  460. {
  461.   
  462.   /* conversion direction depends on 1st arg */
  463.   if (strcmp(argv[1],"a") == 0 || strcmp(argv[1],"A") == 0 )
  464.   {
  465.     *btoa = TRUE;
  466.     *dyn_convert = FALSE;
  467.   }
  468.   else 
  469.     if (strcmp(argv[1],"b") == 0 || strcmp(argv[1],"B") == 0 )
  470.       {
  471.         *atob = TRUE;
  472.         *dyn_convert = NO;
  473.       }
  474.     else
  475.       if (strcmp(argv[1],"d") == 0 || strcmp(argv[1],"D") == 0 )
  476.       {
  477.         *atob = TRUE;
  478.         *dyn_convert = YES;
  479.       }
  480.       else
  481.         if (strcmp(argv[1],"ad") == 0 || strcmp(argv[1],"AD") == 0)
  482.         {
  483.           *btoa = YES;
  484.           *dyn_convert = YES;
  485.         }
  486.         else
  487.           if (strcmp(argv[1],"bd") == 0 || strcmp(argv[1],"BD") == 0)
  488.           {
  489.             *atob = TRUE;
  490.             *dyn_convert = YES;
  491.           }
  492.           else 
  493.           {
  494.              (VOID)fprintf (stderr,
  495.            "%s: first argument must be 'a','b','d','ad' or 'bd' (not '%s').\n",
  496.            ProgName, argv[1]);
  497.              exit(EXIT_ERR);
  498.           }
  499.  
  500.   if ( argc > 4 )
  501.     path = argv[4];
  502.     
  503.   *input_view = argv[2];
  504.   *output_view = argv[3];
  505.  
  506. }    
  507.  
  508.  
  509.  
  510.  
  511.